package com.icesoft.system.controller;

import com.icesoft.framework.core.controller.BaseCrudRestController;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.framework.core.vo.Pagination;
import com.icesoft.system.dto.LoginRecordDTO;
import com.icesoft.system.entity.LoginRecord;
import com.icesoft.system.entity.User;
import com.icesoft.system.permission.OperationCons;
import com.icesoft.system.permission.annotation.BindPermission;
import com.icesoft.system.util.SecurityUtils;
import com.icesoft.system.vo.CompanyVO;
import com.icesoft.system.vo.LoginRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor
@BindPermission(name = "登录记录")
@RequestMapping("/system/login-record")
public class LoginRecordController extends BaseCrudRestController<LoginRecord> {


	/**
	 * 查询ViewObject的分页数据
	 * <p>
	 * url请求参数示例: /list?field=abc&pageSize=20&pageIndex=1&orderBy=id
	 * </p>
	 */
	@GetMapping("/query")
	@BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
	public JsonResult<?> getViewObjectListMapping(LoginRecordDTO entity, Pagination pagination) throws Exception {
		User user = SecurityUtils.getCurrentUser();
		if (user.getId() != 1L) {
			entity.setUserId(user.getId());
		}
		return super.getViewObjectList(entity, pagination, LoginRecordVO.class);
	}

	/**
	 * 根据资源id查询ViewObject
	 *
	 * @param id ID
	 */
	@GetMapping("/{id}")
	@BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
	public JsonResult<?> getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
		return super.getViewObject(id, CompanyVO.class);
	}

	/**
	 * 根据id删除资源对象
	 */
	@GetMapping("/delete/{id}")
	@BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
	public JsonResult<?> deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
		return super.deleteEntity(id);
	}
}
